package com.example.jdbcdemo.service.Impl;

import com.example.jdbcdemo.mapper.*;
import com.example.jdbcdemo.pojo.*;
import com.example.jdbcdemo.service.CollectMoneyOrderServiceInterface;
import com.example.jdbcdemo.utils.CollectMoneyOrderFromFront;
import com.example.jdbcdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CollectMoneyOrderServiceImpl implements CollectMoneyOrderServiceInterface {
//    前端传List<Map>
//    {
//        客户id:  Integer  //这种的可以第一个map解析就行了（根据客户来区分批发与零售）
//        商品id：Integer
//        仓库id：Integer
//        数量  ：Integer
//        售价   : Double
//
//    }
//    修改order、groupidmap、collectmoneyorder、goods

    @Autowired
    private CollectMoneyOrderMapper collectMoneyOrderMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GroupIdMapMapper groupIdMapMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CustomerMapper customerMapper;
//    CollectMoneyOrderFromFront
//    private int customerId;   客户id:
//    private int productId;商品id：
//    private int storageId;仓库id：
//    private int number;数量  ：
//    private double price;售价
//    private int userId;操作员id

    @Override
    public Result<GroupIdMap> addCollectMoneyOrder(int userid, List<CollectMoneyOrderFromFront> collectMoneyOrderFromFront) {
        //前端传来的list大小即为需要新建的order数量
        int orderNumbers=collectMoneyOrderFromFront.size();
        //计算新建GroupIdMap的id
        int groupIdMapId=groupIdMapMapper.queryGroupIdMapMaxId()+1;

        Date date = new Date();
        GroupIdMap groupIdMap=new GroupIdMap(groupIdMapId,orderNumbers,date,1,1);//待审核，销售单
        //将新的GroupIdMap添加到数据库中
        groupIdMapMapper.addGroupIdMap(groupIdMap);

        double totalPrice=0.0;//该销售单的总价
        double totalCost=0.0;//该销售单的总成本
        //遍历前端传过来的list，每一个元素对应一个order，新建order并插入数据库
        for(CollectMoneyOrderFromFront temp:collectMoneyOrderFromFront)
        {
            Customer customer=customerMapper.queryCustomerById(temp.getCustomerId());
            if (customer==null)
            {
                return Result.error(400,"输入了错误的客户id");
            }
            Order order=new Order();
            order.setUserId(userid);
            order.setCustomerId(temp.getCustomerId());
            order.setStorageId(temp.getStorageId());
            order.setProductId(temp.getProductId());
            order.setNumber(temp.getNumber());
            order.setPrice(temp.getPrice());
            order.setGroupId(groupIdMapId);
            totalPrice+=temp.getPrice()*temp.getNumber();
            Product product=productMapper.queryProductById(temp.getProductId());
            totalCost+=product.getCost()*temp.getNumber();
            orderMapper.addOrder(order);
//            private int id;
//            private int userId;//操作员id
//            private int customerId;//客户id或者是供应商id
//            private int storageId;//仓库id
//            private int productId;//涉及的商品id
//            private int number;//商品数量
//            private double price;//进货价，出货价
//            private int groupId;//标识组id

        }
//CollectMoneyOrder
//        privatete int id;
//        private int customerId;//客户id
//        private double totalPrice;//应收
//        private double acceptPrice;//已收
//        private int status;//收款单状态
//        private double profit;//订单利润
//        private int groupId;//标识组id，用来对应前面的Order

        //创建一个收款单
        CollectMoneyOrder collectMoneyOrder=new CollectMoneyOrder();
        collectMoneyOrder.setCustomerId(collectMoneyOrderFromFront.get(0).getCustomerId());
        collectMoneyOrder.setTotalPrice(totalPrice);
        collectMoneyOrder.setAcceptPrice(0.0);
        collectMoneyOrder.setStatus(0);//未收齐
        collectMoneyOrder.setGroupId(groupIdMapId);
        collectMoneyOrder.setProfit(totalPrice-totalCost);

        //将新收款单插入数据库
        collectMoneyOrderMapper.addCollectMoneyOrder(collectMoneyOrder);

        return Result.success();
    }

    @Override
    public Result reviewPass(int groupIdMapId) {
        GroupIdMap groupIdMap=groupIdMapMapper.queryGroupIdMapById(groupIdMapId);
        groupIdMap.setStatus(2);//审核通过
        groupIdMapMapper.updateGroupIdMap(groupIdMap);
        List<Order> orderList=orderMapper.queryOrderList();
        List<Goods> goodsList=goodsMapper.queryGoodsList();
        for(Order order:orderList)
        {
            if(order.getGroupId()==groupIdMapId)
            {
                for(Goods goods:goodsList)
                {
                    if(goods.getProductId()==order.getProductId()&&goods.getStorageId()==order.getStorageId())
                    {
                        Goods goodsModified=goodsMapper.queryGoodsById(goods.getId());
                        //库存数量大于订货数量
                        if(goodsModified.getNumber()>=order.getNumber())
                        {
                            goodsModified.setNumber(goodsModified.getNumber()-order.getNumber());
                            goodsMapper.updateGoods(goodsModified);
                        }
                        else
                        {
                            return Result.error(1,"storage "+goods.getStorageId()+" number "+goods.getNumber()+",product "+goods.getProductId()+" required number "+order.getNumber());
                        }
                    }
                }
            }
        }
        return Result.success();
    }

    @Override
    public Result reviewFail(int groupIdMapId) {
        GroupIdMap groupIdMap=groupIdMapMapper.queryGroupIdMapById(groupIdMapId);
        groupIdMap.setStatus(3);//审核不通过
        groupIdMapMapper.updateGroupIdMap(groupIdMap);
        collectMoneyOrderMapper.deleteCollectMoneyOrderByGroupId(groupIdMapId);
        return Result.success();
    }

    @Override
    public Result pay(int groupIdMapId, double payedMoney) {
        List<CollectMoneyOrder> collectMoneyOrderList=collectMoneyOrderMapper.queryCollectMoneyOrderList();
        for(CollectMoneyOrder collectMoneyOrder:collectMoneyOrderList)
        {
            if(collectMoneyOrder.getGroupId()==groupIdMapId)
            {
                CollectMoneyOrder cmoModified=collectMoneyOrderMapper.queryCollectMoneyOrderById(collectMoneyOrder.getId());
                cmoModified.setAcceptPrice(payedMoney);
                //未收齐
                if(payedMoney<cmoModified.getTotalPrice())
                {
                    cmoModified.setStatus(0);
                }
                else
                {
                    //已收齐
                    cmoModified.setStatus(1);
                }
                collectMoneyOrderMapper.updateCollectMoneyOrder(cmoModified);
                break;
            }
        }
        return Result.success();
    }
}
